<!-- 进销存日报表 -->
<template>
  <div class="app-container">
    <!-- 上部分表单 -->
    <el-form
      class="globalSerchFormStyle"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      label-position="right"
    >
      <el-row style="margin-top: 15px">
        <el-col
          :span="6"
          v-if="['', null].includes($store.state.user.userLevel)"
        >
          <el-form-item label="油企名称" prop="enterpriseId" size="small">
            <el-select
              style="margin-left: 15px"
              v-model="queryParams.enterpriseId"
              :clearable="true"
              filterable
              placeholder="请选择油企"
              @change="handleSelectChange"
            >
              <el-option
                v-for="(item, index) in enterpriseList"
                :key="index"
                :value="item.enterpriseId"
                :label="item.enterpriseName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6" v-if="!['2'].includes($store.state.user.userLevel)">
          <el-form-item
            label="仓库/门店"
            prop="stationId"
            clearable
            size="small"
          >
            <el-select
              style="margin-left: 15px"
              v-model="queryParams.stationId"
              :clearable="true"
              filterable
              placeholder="请选择仓库/门店"
            >
              <el-option
                v-for="item in stationList"
                :key="item.stationId"
                :label="item.stationName"
                :value="item.stationId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="商品编码"
            prop="commodityCode"
            clearable
            size="small"
          >
            <el-input
              style="margin-left: 15px"
              v-model="queryParams.commodityCode"
              placeholder="请输入商品编码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="商品条码" prop="barCode" clearable size="small">
            <el-input
              style="margin-left: 15px"
              v-model="queryParams.barCode"
              placeholder="请输入商品条码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row style="margin-top: 15px" v-show="isHidden">
        <el-col :span="6">
          <el-form-item
            label="商品类别"
            prop="categoryId"
            clearable
            size="small"
          >
            <el-cascader
              style="margin-left: 15px"
              v-model="queryCategoryIds"
              :options="categoryOptions"
              :props="{
                checkStrictly: true,
                label: 'categoryName',
                value: 'categoryId',
                children: 'children',
              }"
              filterable
              clearable
              size="small"
            ></el-cascader>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="查询类型" prop="types" clearable size="small">
            <el-select
              style="margin-left: 15px"
              v-model="queryParams.types"
              :clearable="true"
              filterable
              placeholder="请选择查询类型"
            >
              <el-option label="按明细" value="det"> </el-option>
              <el-option label="按商品" value="com"> </el-option>
              <el-option label="按油站" value="sta"> </el-option>
              <el-option label="按商品类别" value="cat"> </el-option>
              <el-option label="按供应商" value="sup"> </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="查询日期" prop="createdDate" size="small">
            <el-date-picker
              type="datetimerange"
              :default-time="['00:00:00', '23:59:59']"
              style="width: 20px; margin-left: 15px"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dateRangeCreatedDate"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeDateRangeDate"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="global_el_rowStyle" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-show="isHidden"
          type="primary"
          icon="el-icon-arrow-up"
          size="mini"
          @click="isHidden = false"
          >收起</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-show="!isHidden"
          type="primary"
          icon="el-icon-arrow-down"
          size="mini"
          @click="isHidden = true"
          >展开</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          @click="handleExport"
          size="mini"
          plain
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-setting"
          @click="columnSetting"
          size="mini"
          plain
          >列设置</el-button
        >
      </el-col>
    </el-row>

    <el-table style="width: 100%" :data="orderList" v-loading="loading" border>
      <el-table-column
        prop="index"
        label="序号"
        width="60"
        fixed="left"
      ></el-table-column>

      <el-table-column
        v-for="item in invoicingDailyReportList[0]"
        v-if="columnId.includes(item.is)"
        :label="item.label"
        :prop="item.prop"
        fixed="left"
        :formatter="showFotmat"
      >
      </el-table-column>

      <el-table-column label="期初" v-if="columnList[1]">
        <el-table-column
          v-for="item in invoicingDailyReportList[1]"
          v-if="columnId.includes(item.is)"
          :label="item.label"
          :prop="item.prop"
          :formatter="showFotmat"
        >
        </el-table-column>
      </el-table-column>

      <el-table-column label="本期(+)" v-if="columnList[2]">
        <el-table-column
          v-for="item in invoicingDailyReportList[2]"
          v-if="columnId.includes(item.is)"
          :label="item.label"
          :prop="item.prop"
          :formatter="showFotmat"
        >
        </el-table-column>
      </el-table-column>

      <el-table-column label="本期(-)" v-if="columnList[3]">
        <el-table-column
          v-for="item in invoicingDailyReportList[3]"
          v-if="columnId.includes(item.is)"
          :label="item.label"
          :prop="item.prop"
          :formatter="showFotmat"
        >
        </el-table-column>
      </el-table-column>

      <el-table-column label="本期调整(+)" v-if="columnList[4]">
        <el-table-column
          v-for="item in invoicingDailyReportList[4]"
          v-if="columnId.includes(item.is)"
          :label="item.label"
          :prop="item.prop"
          :formatter="showFotmat"
        >
        </el-table-column>
      </el-table-column>

      <el-table-column label="库存损益(+)" v-if="columnList[5]">
        <el-table-column
          v-for="item in invoicingDailyReportList[5]"
          v-if="columnId.includes(item.is)"
          :label="item.label"
          :prop="item.prop"
          :formatter="showFotmat"
        >
        </el-table-column>
      </el-table-column>

      <el-table-column label="结存" v-if="columnList[6]">
        <el-table-column
          v-for="item in invoicingDailyReportList[6]"
          v-if="columnId.includes(item.is)"
          :label="item.label"
          :prop="item.prop"
          :formatter="showFotmat"
        >
        </el-table-column>
      </el-table-column>

      <el-table-column
        v-for="item in invoicingDailyReportList[7]"
        v-if="columnId.includes(item.is)"
        :label="item.label"
        :prop="item.prop"
        :formatter="showFotmat"
      >
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 列设置 -->
    <InvoicingSetting
      :drawerSetting.sync="drawerSetting"
      @confirm="confirm"
    ></InvoicingSetting>
  </div>
</template>

<script>
import {
  enterpriseList,
  listEnterpriseStation,
} from "@/api/nonOil/putlntoStorage";
import {
  customListColumnGet,
  customListColumnSave,
  inventoryReportDailyList,
  inventoryReportDailySum,
} from "@/api/nonOil/statement";
import { InvoicingDailyReport } from "@/utils/dataReport";
import InvoicingSetting from "./InvoicingSetting.vue";
import { getNonOilCategoryTreeApi } from "@/api/nonOil/class";

export default {
  name: "InvoicingDailyReport",
  components: { InvoicingSetting },
  data() {
    return {
      queryCategoryIds: [],
      // 所属分类列表
      categoryOptions: [],
      // 列表全部数据
      invoicingDailyReportList: [],
      // 展开收起
      isHidden: false,
      // 设置抽提
      drawerSetting: false,
      // 油企
      enterpriseList: [],
      // 油站
      stationList: [],
      //时间间隔值
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,

      //表格数据
      orderList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        stationId: null,
        // 商品编码
        commodityCode: null,
        // 商品条码
        barCode: null,
        // 商品类别
        categoryId: null,
        // 查询类型
        types: null,
        // 开始时间
        beginTime: null,
        // 截止时间
        endTime: null,
      },

      loading: false,

      columnId: [],

      // “-”数组
      citiesList: [],

      // 表格
      columnList: [],

      // “-”数组以外
      citiesListOther: [],
    };
  },
  created() {
    this.invoicingDailyReportList = InvoicingDailyReport;
    this.getCitiesList();
    this.getList();
    this.getEnterpriseList();
    this.customListColumn();
    this.getNonOilCategoryList();
    // 油企
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.queryParams.enterpriseId = this.presentEnterprise.enterpriseId;
      this.getStationList(this.queryParams.enterpriseId);
      return;
    }
    // 油站账号登录
    if (this.presentSation) {
      this.queryParams.stationId = this.presentSation;
      this.handleQuery();
      return;
    }
  },

  methods: {
    // 获取分类列表
    async getNonOilCategoryList() {
      const res = await getNonOilCategoryTreeApi();
      this.categoryOptions = res.data.children;
      this.getNonOilData(this.categoryOptions);
    },

    getNonOilData(categoryOptions) {
      categoryOptions.forEach((item) => {
        if (!item.children.length) {
          item.children = null;
        } else {
          this.getNonOilData(item.children);
        }
      });
    },

    // 获取“-”
    getCitiesList() {
      this.citiesList = [];
      const citiesList = [
        ...InvoicingDailyReport[0],
        ...InvoicingDailyReport[1],
        ...InvoicingDailyReport[2],
        ...InvoicingDailyReport[3],
        ...InvoicingDailyReport[4],
        ...InvoicingDailyReport[5],
        ...InvoicingDailyReport[6],
        ...InvoicingDailyReport[7],
      ];

      citiesList.forEach((item) => {
        if (item.show === false) {
          this.citiesList.push(item.label);
        } else {
          this.citiesListOther.push(item.is);
        }
      });
    },

    // 设置“-”
    showFotmat(row, column) {
      if (this.citiesList.includes(column.label)) {
        return "-";
      } else {
        return row[column.property];
      }
    },

    // 获取列表
    async customListColumn() {
      const result = await customListColumnGet({ tableId: 3 });

      if (!result.data) {
        customListColumnSave({
          tableId: 3,
          columnId: this.citiesListOther.join(","),
        }).then((res) => {
          this.customListColumn();
          return;
        });
      } else {
        this.columnId = result.data.columnId.split(",");
        this.invoicingDailyReportList.forEach((itema, index) => {
          this.columnList[index] = itema.some((item) =>
            this.columnId.includes(item.is)
          );
        });
      }
    },

    // 点击列设置
    columnSetting() {
      this.drawerSetting = true;
    },

    // 选择了时间
    changeDateRangeDate() {
      // 开始时间
      this.queryParams.beginTime = this.dateRangeCreatedDate
        ? this.dateRangeCreatedDate[0]
        : null;
      // 截止时间
      this.queryParams.endTime = this.dateRangeCreatedDate
        ? this.dateRangeCreatedDate[1]
        : null;
    },

    // 请选择油企
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },

    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },

    // 查询油站
    getStationList(val) {
      listEnterpriseStation({ enterpriseId: val, status: "1" }).then((res) => {
        this.stationList = res;
      });
    },

    // 搜索列表
    getList() {
      this.loading = true;
      let queryParams = this.queryParams;

      // 处理搜索条件中的分类id
      if (this.queryCategoryIds.length) {
        let ids = this.queryCategoryIds;
        queryParams.categoryId = ids[ids.length - 1];
      } else {
        queryParams.categoryId = null;
      }

      inventoryReportDailyList(queryParams)
        .then((response) => {
          this.loading = false;
          this.orderList = response.rows;
          this.orderList.forEach((item, index) => {
            item.index = index + 1;
          });
          this.total = response.total;
          this.getInventoryReportDailySum();
        })
        .catch(() => {
          this.loading = false;
          this.orderList = [];
          this.total = 0;
        });
    },

    confirm(val) {
      this.drawerSetting = false;
      this.getList();
      this.customListColumn();
    },

    // 合计列表
    async getInventoryReportDailySum() {
      let queryParams = this.queryParams;
      const result = await inventoryReportDailySum(queryParams);
      this.orderList.push({ ...result.data, index: "合计" });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      // 查询参数
      this.queryCategoryIds = [];
      this.queryParams.categoryId = null;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.dateRangeCreatedDate = [];
      // 开始时间
      this.queryParams.beginTime = null;
      // 截止时间
      this.queryParams.endTime = null;
      this.resetForm("queryForm");
      this.getList();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;

      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          // return exportBusinessDailyReportList(queryParams);
        })
        .then((response) => {
          // this.download(response.msg);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.tree-item {
  border: 1px solid #e7e7e7;
  width: 200px;
}

.div-tree {
  display: flex;
}

.app-container {
  margin-top: -15px;
}

::v-deep .card-table {
  padding: 10px;
  margin-top: 0 !important;
  background-color: #f8f8f8;

  .el-button {
    margin: 0 5px 0 0;
  }
}

// 表格样式
::v-deep .el-form-item {
  margin-bottom: 0;

  .el-form-item__label {
    padding: 0;
    margin: 0;
    height: 29px;
    font-weight: 400;
    font-size: 12px;
  }
}

::v-deep .el-table {
  border: 1px #f8f8f8 solid;
  border-bottom: 1px #dfe6ec solid;
}

::v-deep .is-center {
  border: 1px transparent solid;
  border-bottom: 1px #f8f8f8 solid;
}
</style>
